﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNADash.Textures;
using XNADash.Sound;

namespace XNADash.BoardBlocks
{
    public class BalloonBlock : FallingBlock
    {
        protected override GameTexture BlockTexture
        {
            get
            {
                return GameTexture.Balloon;
            }
        }

        public override bool CanBePushed
        {
            get
            {
                return true;
            }
        }

        public override bool OthersFallFrom
        {
            get
            {
                return false;
            }
        }

        public override void ApplyPhysics()
        {
            BaseBlock s1 = this.GetNeighbour( Directions.S );
            BaseBlock n1 = this.GetNeighbour( Directions.N );

            // ruch do góry - nic nad nim
            if ( n1 == null && !this.IsFalling )
            {
                this.MoveTo( Directions.N );

                return;
            }

            // ruch do góry - jeden nad nim i nic jeszcze wyżej (unosi coś)
            if ( n1 != null && !this.IsFalling )
            {
                BaseBlock n2 = n1.GetNeighbour( Directions.N );

                if ( 
                     ( n1.CanBeLifted || n1 is PlayerBlock ) && 
                      !(n1 is BoomBlock) &&
                       n2 == null 
                    )
                {
                    n1.MoveTo( Directions.N );                    
                    this.MoveTo( Directions.N );

                    return;
                }
            }

            // spad w dół - dwa nad nim i nic pod spodem
            if ( s1 == null && n1 != null && !( n1 is BalloonBlock ) && !( n1 is BoomBlock ) )
            {
                BaseBlock n2 = n1.GetNeighbour( Directions.N );
                if ( n2 != null && !( n2 is BalloonBlock ) && !( n2 is BoomBlock ) )
                {
                    if ( n1.CanBeLifted && n2.CanBeLifted )
                    {
                        this.MoveTo( Directions.S );
                        n1.MoveTo( Directions.S );
                        n2.MoveTo( Directions.None ); // oznacz jako poruszony

                        this.IsFalling = true;

                        SoundFactory.Instance.RegisterEffect( SoundType.Balloon );
                    }

                    return;
                }
            }

            this.IsFalling = false;

            return;
        }

        public override string ToString()
        {
            return "0";
        }
    }
}
